package com.template.controller;

import com.template.common.result.Result;
import com.template.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Redis测试控制器
 * 
 * @author template
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/redis")
public class RedisTestController {

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 测试Redis连接
     */
    @GetMapping("/test")
    public Result<String> testRedis() {
        try {
            // 设置一个测试值
            redisUtil.set("test:connection", "Redis连接成功！", 60);
            
            // 获取测试值
            Object value = redisUtil.get("test:connection");
            
            return Result.success("Redis连接测试成功：" + value);
        } catch (Exception e) {
            return Result.error("Redis连接测试失败：" + e.getMessage());
        }
    }

    /**
     * 设置字符串值
     */
    @PostMapping("/set")
    public Result<String> setValue(@RequestParam String key, @RequestParam String value, @RequestParam(defaultValue = "0") long expire) {
        try {
            boolean success = redisUtil.set(key, value, expire);
            if (success) {
                return Result.success("设置成功");
            } else {
                return Result.error("设置失败");
            }
        } catch (Exception e) {
            return Result.error("设置失败：" + e.getMessage());
        }
    }

    /**
     * 获取字符串值
     */
    @GetMapping("/get")
    public Result<Object> getValue(@RequestParam String key) {
        try {
            Object value = redisUtil.get(key);
            return Result.success(value);
        } catch (Exception e) {
            return Result.error("获取失败：" + e.getMessage());
        }
    }

    /**
     * 删除键
     */
    @DeleteMapping("/del")
    public Result<String> deleteKey(@RequestParam String key) {
        try {
            redisUtil.del(key);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 设置Hash值
     */
    @PostMapping("/hset")
    public Result<String> setHashValue(@RequestParam String key, @RequestParam String field, @RequestParam String value) {
        try {
            boolean success = redisUtil.hset(key, field, value);
            if (success) {
                return Result.success("Hash设置成功");
            } else {
                return Result.error("Hash设置失败");
            }
        } catch (Exception e) {
            return Result.error("Hash设置失败：" + e.getMessage());
        }
    }

    /**
     * 获取Hash值
     */
    @GetMapping("/hget")
    public Result<Object> getHashValue(@RequestParam String key, @RequestParam String field) {
        try {
            Object value = redisUtil.hget(key, field);
            return Result.success(value);
        } catch (Exception e) {
            return Result.error("Hash获取失败：" + e.getMessage());
        }
    }

    /**
     * 测试对象存储
     */
    @PostMapping("/test/object")
    public Result<String> testObjectStorage() {
        try {
            Map<String, Object> user = new HashMap<>();
            user.put("id", 1);
            user.put("name", "测试用户");
            user.put("email", "test@example.com");
            
            // 存储对象
            redisUtil.set("user:1", user, 300);
            
            // 获取对象
            Object retrievedUser = redisUtil.get("user:1");
            
            return Result.success("对象存储测试成功：" + retrievedUser);
        } catch (Exception e) {
            return Result.error("对象存储测试失败：" + e.getMessage());
        }
    }
}
